package com.hzqy.service.epg;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.WChatUtils;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

@Service
public class WchatService {
    private static final Log LOG = LogFactory.getLog(WchatService.class);

    private final static String TOKEN_KEY = "accessToken";//redis存储TOKEN的KEY
    private final static int TOKEN_TIME = 7000;//TOKEN保存时间
    private final static int QRCODE_TIME = 302400;//临时二维码保存12小时

    public static String getAccessToken() throws IOException {
        String accessToken = RedisService.getAccessToken(TOKEN_KEY);
        if(accessToken == null || accessToken.equals("null") || accessToken.equals("")){
            JSONObject jsonStr = JSONObject.parseObject(WChatUtils.getAccessToken());
            accessToken = jsonStr.getString("access_token");
            RedisService.setAccessToken(TOKEN_KEY,accessToken,TOKEN_TIME);
        }
        return accessToken;
    }

    public static String getWebAccessToken(String code){
        JSONObject jsonStr = JSONObject.parseObject(WChatUtils.getWebAccessToken(code));
        return jsonStr.getString("openid");
    }

    public static String getQRCode(String serverId) throws IOException {
        String accessToken = getAccessToken();
        return WChatUtils.getQRSCENE(accessToken,QRCODE_TIME,serverId);
    }

    public static String getORPIC(HttpSession session, String serverId) throws IOException {
        JSONObject jsonStr = JSONObject.parseObject(getQRCode(serverId));
        String ticket = jsonStr.getString("ticket");
        return WChatUtils.getQRPIC(ticket);
    }

	public static String createSignUp() throws IOException {
		String accessToken = getAccessToken();
		//String jsonStr = "{\"button\":[{\"type\":\"view\",\"name\":\"斗地主大赛报名\",\"url\":\""+WChatUtils.getDomain()+"gd_service/activity/20190318ddzds/mobile/index.jsp\"}]}";
		//String jsonStr = "{\"button\":[{\"type\":\"view\",\"name\":\"话费兑换\",\"url\":\""+WChatUtils.getDomain()+"gd_service/activity/20190318ddzds/mobile/indexPhone.jsp\"}]}";
		//String jsonStr = "{\"button\": [{\"type\":\"view\",\"name\":\"斗地主大赛报名\",\"url\":\""+WChatUtils.getDomain()+"/gd_service/activity/20190318ddzds/mobile/index.jsp\"}, {\"type\":\"view\",\"name\":\"十元话费领取\",\"url\":\""+WChatUtils.getDomain()+"gd_service/activity/20190318ddzds/mobile/indexPhone.jsp\"}]}";
		String jsonStr = "{\"button\": [{\"type\":\"view\",\"name\":\"广场舞赠票\",\"url\":\""+WChatUtils.getDomain()+"/gd_service/activity/20190410gcw/mobile/indexTwo.jsp\"}, {\"type\":\"view\",\"name\":\"智者为王\",\"url\":\""+WChatUtils.getDomain()+"/gd_service/activity/20190531zzww/mobile/index.jsp\"}]}";
		return WChatUtils.createButton(accessToken,jsonStr);
	}
	
	public static String delButton() throws IOException {
		String accessToken = getAccessToken();
		return WChatUtils.delButton(accessToken);
	}

    public static String getServerQRCode(String picServerUrl){
        return WChatUtils.sendGet(picServerUrl);
    }

    public static String getDomain(){
        return WChatUtils.getDomain();
    }
    public static String getAPPID(){
        return WChatUtils.getAPPID();
    }

    public static String getUserInfoFromBasisToken(String openid) throws IOException {
        String accessToken = getAccessToken();
        return WChatUtils.getUserInfoFromBasisToken(accessToken, openid);
    }
    
    public static void main(String[] args) {

    }
}
